#ifndef CONFIG__H
#define CONFIG__H

#include "struct.h"

config_t config = {};

#include "utils.h"
#include "audit.h"

/**
 * @description: 通过字符串缺点审计等级
 * @param {char} *log_level
 * @return {*}
 */
int set_log_level(char *log_level)
{
    int ret = 0;
    if (strcmp(log_level, "DEBUG") == 0)
    {
        config.log_level = AUDIT_LEVEL_DEBUG;
    }
    else if (strcmp(log_level, "LOG") == 0)
    {
        config.log_level = AUDIT_LEVEL_LOG;
    }
    else if (strcmp(log_level, "NONE") == 0)
    {
        config.log_level = AUDIT_LEVEL_NONE;
    }
    else
    {
        ebpflsm_error("解析审计等级出现问题，错误的审计信息:%s", log_level);
        ret = -1;
    }

    return ret;
}

/**
 * @description: 配置文件路径解析，输出到config结构体中
 * @param {char} *config_path
 * @param {config_t} *config
 * @return {*}
 */
int config_setup(char *config_path)
{
    int ret = 0;
    char log_level[32];

    // 策略相关信息配置
    ret = parse_yaml(config_path, "policy_dir", config.policy_path);
    if (ret < 0)
    {
        ebpflsm_error("解析策略文件出错，无法读取策略路径：%s", config.policy_path);
    }

    // 审计相关信息配置
    ret = parse_yaml(config_path, "log_path", config.log_path);
    if (ret < 0)
    {
        ebpflsm_error("解析审计日志路径出错，无法读取审计路径：%s", config.log_path);
    }

    // 审计等级
    ret = parse_yaml(config_path, "log_level", log_level);
    if (ret < 0)
    {
        ebpflsm_error("解析审计等级出错，无法读取审计路径：%s", config.log_level);
    }
    ret = set_log_level(log_level);
    if (ret < 0)
    {
        ebpflsm_info("恢复使用默认审计等级:DEBUG");
        config.log_level = AUDIT_LEVEL_DEBUG;
    }

    return ret;
}

#endif //! CONFIG__H